// pages/rigister/index.js
import {login} from '../../apis/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    partyData: {},
    phoneNum: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('options----', options)
    const partyData = JSON.parse(decodeURIComponent(options.partyData))
    this.setData({
      partyData:partyData
    })
  },
  getCode(){
    return new Promise((resolve, reject) => {
      wx.login({
        success(res){
         console.log("code的值是", res.code)
         resolve(res.code)
        }
     })
    })
  },
  async startLogin(){
    /*
    form:{"code":"073mzx0w3XnHzZ21O54w3Qid4r2mzx0J","isParty":false,"mobile":"18822898732","partyData":{"code":"0636Im1w3OC4BZ2ix32w3Q76AI36Im19","oauth":"MP-WEIXIN","userInfo":{"nickName":"githuo","gender":0,"language":"zh_CN","city":"","province":"","country":"","avatarUrl":"https://thirdwx.qlogo.cn/mmopen/vi_32/mJgy1BfjtjPYvqJhzBsEW9EthxGot7j6p4ckTdQ94Ifbl5hxzyOvFqPQCBWgm7NiagXEEg3s7MEBjPR3hmfysaw/132"}}}
    */ 
    const code = await this.getCode()
   const data = {
     form: {
       code: code,
       "isParty":false,
       mobile: this.data.phoneNum,
       partyData: this.data.partyData
     }
   }
   console.log(data)
   const loginResult = await login({data:data})
   console.log("登录注册结果", loginResult)
   if (loginResult.status === 200) {
      wx.setStorageSync('token', loginResult.data.token)
      wx.setStorageSync('userInfo', this.data.partyData.userInfo)
      wx.switchTab({
        url: '/pages/profile/index',
      })
   }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})